<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\City;
use App\Services\Curl;
use Illuminate\Http\Request;

class GetLocationController extends Controller
{
    public function getArea(Request $request)
    {
        $city_name = $request->get('city');
        $cityData = City::getAreaList($city_name);
        if (!$cityData){
            return ['status'=>10002,'msg'=>'暂无数据','data'=>[]];
        }
        $data = array_unique($cityData->toArray());
        $info = [];
        foreach ($data as $k => $v)
        {
            $arr = [
                'id' => $k,
                'area_name' => $v,
                'class' => 'chunk'
            ];
            array_push($info,$arr);
        }
        return $info;
    }

    public function getHouses(Request $request)
    {
        $area_name = $request->get('area_name');
        $keyword = '住宅区';
        $request_url = sprintf(config('tencent_map.suggestion_url'),$area_name,$keyword,config('tencent_map.key'));
        $housesData = Curl::getRequest($request_url);
        $data = [];
        foreach ($housesData['data'] as $k => $v)
        {
            $arr = [
                'id' => $k,
                'houses_name' => $v['title'],
                'class' => 'chunk'
            ];
            array_push($data,$arr);
        }
        return $data;
    }
}
